package test.gravity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.shapes.OvalShape;
import android.view.View;
import android.view.WindowManager;

public class DrawView extends View {
	public Ball Ball1, Ball2;
	public Ball[] Balls;
	private Paint paint1, paint2;
	private Context myContext;
	public int MAX_X, MAX_Y;
	public DrawView(Context cxt) {
		super(cxt);
		setFocusableInTouchMode(true);
		WindowManager manager = (WindowManager) cxt.getSystemService(Context.WINDOW_SERVICE);
		MAX_X= manager.getDefaultDisplay().getWidth();
		MAX_Y=724;
		System.out.println("MAX_X="+MAX_X+"\tMAX_Y="+MAX_Y);
		Ball1 = new Ball(cxt, this, 200, 500, 15);
		Ball2 = new Ball(cxt, this, 80, 400, 5);
		Ball[] tBalls = {Ball1, Ball2};
		Balls=tBalls;
		paint1 = new Paint();
		paint1.setColor(Color.RED);
		paint1.setAntiAlias(true);
		paint2 = new Paint();
		paint2.setColor(Color.LTGRAY);
		paint2.setAntiAlias(true);
		myContext=cxt;
	}
	protected void onDraw(Canvas canvas) {
		repel();
		Ball1.step();
		Ball2.step();
		bounces();
		canvas.drawColor(Color.BLUE);
		canvas.drawCircle((float) Ball1.getX(), (float) Ball1.getY(), (float) Ball1.getRadius(), paint1);
		canvas.drawCircle((float) Ball2.getX(), (float) Ball2.getY(), (float) Ball2.getRadius(), paint2);
		//canvas.drawCircle(50, 50, 5, paint1);
		invalidate();
	}
	private void repel() {
		double magnitude, dist;
		dist=distance(Ball1.getX(), Ball1.getY(), Ball2.getX(), Ball2.getY());
		magnitude=150.0/(Math.pow(dist, 2.0)+Ball1.getRadius()+Ball2.getRadius());
		Ball1.setXV(Ball1.getXV()+(Ball1.getX()-Ball2.getX())*magnitude/dist);
		Ball1.setYV(.2+Ball1.getYV()+(Ball1.getY()-Ball2.getY())*magnitude/dist);
		Ball2.setXV(Ball2.getXV()+(Ball2.getX()-Ball1.getX())*magnitude/dist);
		Ball2.setYV(.2+Ball2.getYV()+(Ball2.getY()-Ball1.getY())*magnitude/dist);
	}
	private void bounces() {
		if (false && distance(Ball1.getX(), Ball1.getY(), Ball2.getX(), Ball2.getY())<=Ball1.getRadius()+Ball2.getRadius()) {
			double angle = Math.atan((Ball2.getY()-Ball1.getY())/(Ball2.getX()-Ball1.getX()));
		} else {
			for (Ball b : Balls) {
				//System.out.println("Radius:"+(int)b.getRadius()+"\tXV:"+(int)b.getXV()+"\tYV:"+(int)b.getYV());
				double excessPartOfMove;
				if (b.getY()+b.getRadius()>=MAX_Y) {
					excessPartOfMove=(b.getY()-MAX_Y+b.getRadius())/(b.getYV());
					b.setY(MAX_Y-b.getRadius()-.01);
					b.setX(b.getX()-excessPartOfMove*b.getXV());
					b.setYV(bounceVelocity(b.getYV(), false));
				} else if (b.getY()-b.getRadius()<=0) {
					excessPartOfMove=(b.getRadius()-b.getY())/(b.getYV());
					b.setY(b.getRadius()+.01);
					//b.setX(b.getX()-excessPartOfMove*b.getXV());
					b.setYV(bounceVelocity(b.getYV(), true));
				} 
				if (b.getX()+b.getRadius()>=MAX_X) {
					excessPartOfMove=(b.getX()-MAX_X+b.getRadius())/(b.getXV());
					b.setX(MAX_X-b.getRadius()-.01);
					b.setY(b.getY()-excessPartOfMove*b.getYV());
					b.setXV(bounceVelocity(b.getXV(), false));
				} else if (b.getX()-b.getRadius()<=0) {
					excessPartOfMove=(b.getRadius()-b.getX())/(b.getXV());
					b.setX(b.getRadius()+.01);
					b.setY(b.getY()-excessPartOfMove*b.getYV());
					b.setXV(bounceVelocity(b.getXV(), true));
				}
			}
		}
	}
	private double bounceVelocity(double initV, boolean toPositive) {
		if (toPositive && initV<0) {
			return initV*-0.96;
		} else if (toPositive) {
			return initV*0.96;
		} else if (initV<0) {
			return initV*0.96;
		} else {
			return initV*-0.96;
		}
	}
	private double distance(double x1, double y1, double x2, double y2) {
		double c2 = (x2-x1)*(x2-x1)+(y2-y1)*(y2-y1);
		return Math.pow(c2, 1.0/2.0);
	}
}
